<?php
namespace App\Console\Commands;

use App\Models\SwtPledgeOrder;
use App\Models\SwtTradeAddress;
use Illuminate\Console\Command;

class PledgeOrderProfit extends Command
{
    protected $signature = 'pledge_order_profit:worker';

    protected $description = '每日质押订单收益增加';

    public function handle()
    {
        $order_list = SwtPledgeOrder::where('status',1) -> get();

        $day = date('ymd', time());

        foreach($order_list as $order) {
            $order_end_time = date('ymd', strtotime($order -> end_time));

            if($order_end_time <= $day) {
                $this -> finishPledgeOrder($order);
            } else {
                $this -> payPledgeOrderProfit($order);
            }
        }

        return true;
    }

    public function finishPledgeOrder($order)
    {
        $total = bcadd($order -> total, $order -> income_amount, 5);

        $info = SwtTradeAddress::find($order -> trade_id);

        $info -> total = bcadd($info -> total, $total,5);

        $info -> lock_total = bcsub($info -> lock_total, $order -> total, 5);

        $info -> save();

        $order -> status = 3;

        $order -> save();
    }

    public function payPledgeOrderProfit($order)
    {
        $income_amount = $order -> total * $order -> year_rate / 100 / $order -> lock_day;

        $order -> income_amount = bcadd($order -> income_amount, $income_amount, 5);

        $order -> save();
    }
}
